package com.sea.supply.service.impl;

import com.codecrane.core.util.CommonUtils;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.data.entity.Order;
import com.sea.data.entity.OrderDetail;
import com.sea.operate.entity.RelatedSku;
import com.sea.operate.service.RelatedSkuService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.supply.entity.Supplier;
import com.sea.supply.entity.SupplierSku;
import com.sea.supply.mapper.SupplierMapper;
import com.sea.supply.mapper.SupplierSkuMapper;
import com.sea.supply.service.SupplierService;
import com.sea.supply.service.SupplierSkuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class SupplierSkuServiceImpl implements SupplierSkuService {

    @Autowired
    private SupplierSkuMapper supplierSkuMapper;
    @Autowired
    private RelatedSkuService relatedSkuService;


    @Override
    public List<SupplierSku> findByCnd(SupplierSku supplierSku) {
        return supplierSkuMapper.findByCnd(supplierSku);
    }

    @Override
    public Integer save(SupplierSku supplierSku) {
        return supplierSkuMapper.insert(supplierSku);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<SupplierSku> list = BatchImportUtils.iteratorForSupplierSku(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {

                clearSupplierSku();
                for (SupplierSku supplierSku : list) {
//                    RelatedSku relatedSku = relatedSkuService.findByChannelSku(supplierSku.getSupplierChannelName(), supplierSku.getSupplierSku());
//                    if(null==relatedSku){
//                        throw new RuntimeException("暂未查询到对应表！渠道【"+supplierSku.getSupplierChannelName()+"】SKU【"+supplierSku.getSupplierSku()+"】");
//                    }
                    SupplierSku skuChannel = findBySkuChannel(supplierSku);
                    if(null==skuChannel){
                        save(supplierSku);
                    }else{
                        usave(supplierSku);
                    }
                }
            }
        }
    }

    @Override
    public SupplierSku findBySkuChannel(SupplierSku supplierSku) {
        return supplierSkuMapper.findBySkuChannel(supplierSku);
    }

    @Override
    public Integer usave(SupplierSku supplierSku) {
        return supplierSkuMapper.update(supplierSku);
    }

    @Override
    public Integer delete(Integer id) {
        return supplierSkuMapper.delete(id);
    }

    @Override
    public Integer clearSupplierSku() {
        return supplierSkuMapper.clearSupplierSku();
    }
}
